package com.chendx.store.goods.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.common.enums.AppHttpCodeEnum;
import com.chendx.model.goods.dto.SpuDto;
import com.chendx.model.goods.dto.SpuInfoDto;
import com.chendx.model.goods.pojo.Category;
import com.chendx.model.goods.pojo.Spu;
import com.chendx.model.goods.vo.SpuInfoVo;
import com.chendx.store.goods.mapper.CategoryMapper;
import com.chendx.store.goods.mapper.SpuMapper;
import com.chendx.store.goods.service.SpuService;
import com.chendx.store.goods.util.GoodsUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chendx on 2022/4/18.
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper,Spu> implements SpuService{


    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResponseResult spuInfos(SpuDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        List<Spu> skuList = list(Wrappers.<Spu>lambdaQuery().eq(Spu::getIsDelete,"0").eq(Spu::getIsMarketable,"0"));
        List<SpuInfoVo> spuInfoVos = new ArrayList<>();
        for (Spu spu : skuList){
            SpuInfoVo spuInfoVo = new SpuInfoVo();
            BeanUtils.copyProperties(spu,spuInfoVo);
            spuInfoVo.setId(String.valueOf(spu.getId()));

            Category category1 = categoryMapper.selectById(spu.getCategory1Id());
            spuInfoVo.setCategory1Name(category1.getName());
            Category category2 = categoryMapper.selectById(spu.getCategory2Id());
            spuInfoVo.setCategory2Name(category2.getName());
            Category category3 = categoryMapper.selectById(spu.getCategory3Id());
            spuInfoVo.setCategory3Name(category3.getName());
            spuInfoVos.add(spuInfoVo);
        }
        return ResponseResult.okResult(spuInfoVos);
    }

    @Override
    public ResponseResult spuInfo(String id) {
        Spu spu = getById(id);
        SpuInfoVo spuInfoVo = new SpuInfoVo();
        BeanUtils.copyProperties(spu,spuInfoVo);
        spuInfoVo.setId(String.valueOf(spu.getId()));

        Category category1 = categoryMapper.selectById(spu.getCategory1Id());
        spuInfoVo.setCategory1Name(category1.getName());
        Category category2 = categoryMapper.selectById(spu.getCategory2Id());
        spuInfoVo.setCategory2Name(category2.getName());
        Category category3 = categoryMapper.selectById(spu.getCategory3Id());
        spuInfoVo.setCategory3Name(category3.getName());
        return ResponseResult.okResult(spuInfoVo);
    }

    @Override
    public ResponseResult add(SpuInfoDto dto) {
        if (dto == null || dto.getName() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Spu spu = new Spu();
        BeanUtils.copyProperties(dto,spu);
       // BeanUtils.copyProperties(spu,dto);
        if (dto.getSpecList() != null && dto.getSpecList().size() > 0){
            String specItems = GoodsUtils.list2Spec(dto.getSpecList());
            spu.setSpecItems(specItems);
        }
        spu.setSn("NO.100010");
        Category category = categoryMapper.selectById(dto.getCategory3Id());


        Integer templateId = category.getTemplateId();
        if (templateId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.CATEGORY_NOT_TEMPLATE);
        }
        spu.setTemplateId(templateId);
        spu.setParaItems("{\"出厂年份\":\"2021\",\"版本\":\"10\"}");
        spu.setSaleNum(0);
        spu.setCommentNum(0);
        spu.setIsMarketable(String.valueOf(0));
        spu.setIsEnableSpec("1");
        spu.setIsDelete("0");
        spu.setStatus("0");
        boolean save = save(spu);
        if (save){
            return ResponseResult.okResult(spu);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }
}
